package xyz.hzxiao.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import xyz.hzxiao.entity.order.DemoOrderEntity;
import xyz.hzxiao.entity.user.DemoUserEntity;
import xyz.hzxiao.remote.order.OrderFeignClient;
import xyz.hzxiao.result.JsonResult;
import xyz.hzxiao.user.mapper.DemoUserMapper;
import xyz.hzxiao.user.service.DemoUserService;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author FuXx
 * @since 2023-12-05
 */
@Service
@RequiredArgsConstructor
public class DemoUserServiceImpl extends ServiceImpl<DemoUserMapper, DemoUserEntity> implements DemoUserService {

    private final OrderFeignClient orderFeignClient;

    @Override
    public JsonResult<DemoUserEntity> login(String userName, String pass) {
        DemoUserEntity user = this.getOne(new LambdaQueryWrapper<DemoUserEntity>()
                .eq(DemoUserEntity::getUserName, userName)
                .last(" LIMIT 1"));
        Assert.notNull(user, "用户不存在");
        Assert.isTrue(user.getPassworld().equals(pass), "密码错误");
        return JsonResult.ok(user);
    }

    @Override
    public JsonResult<List<DemoOrderEntity>> queryOrdersByUserId(Integer userId) {
        return orderFeignClient.orderList(userId);
    }

    @Override
    public JsonResult<List<DemoUserEntity>> getByOptionNumber(String optionNumber) {
        List<DemoUserEntity> userEntityList = this.list(new LambdaQueryWrapper<DemoUserEntity>()
                .likeRight(Objects.nonNull(optionNumber), DemoUserEntity::getOptionNumber, optionNumber));
        return JsonResult.ok(userEntityList);
    }
}
